package dev.zeno.booru.db;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import dev.zeno.booru.Booru;

public class BooruDBAdapter extends BaseDBAdapter {

	public static final String KEY_NAME = "NAME", KEY_URL = "URL", KEY_FAVICON = "FAVICON", KEY_ID= "ID", KEY_TYPE="TYPE", KEY_USERNAME="USERNAME", KEY_PASSWORD="PASSWORD";
	public static final String DATABASE_NAME = "BOORU_DB";
	public BooruDBAdapter(Context context) {
		
		super(context, DATABASE_NAME, getTypes());
	}

	private static Map<String, String> getTypes()
	{
		Map<String, String> types = new HashMap<String, String>();
		
		types.put(KEY_ID, "INTEGER PRIMARY KEY");
		types.put(KEY_NAME, "TEXT");
		types.put(KEY_URL, "TEXT");
		types.put(KEY_FAVICON, "TEXT");
		types.put(KEY_TYPE, "INTEGER");
		types.put(KEY_USERNAME, "TEXT");
		types.put(KEY_PASSWORD, "TEXT");
		
		return types;
	}
	
	public long delete(int id)
	{
		Map<String, String> where = new HashMap<String, String>();
		where.put(KEY_ID, ""+id);
		return delete(where);
	}
	
	public Booru getBooru(int id)
	{
		Map<String, String> where = new HashMap<String, String>();
		where.put(KEY_ID, String.valueOf(id));
		Cursor all = fetch(where);
		Booru boo = null;
		if(!all.isAfterLast())
		{
			boo = booruFromCursor(all);
		}
		return boo;
	}

	private Booru booruFromCursor(Cursor cur) {
		return new Booru(cur.getInt(cur.getColumnIndex(KEY_ID)), cur.getString(cur.getColumnIndex(KEY_NAME)), cur.getString(cur.getColumnIndex(KEY_URL)),
				cur.getString(cur.getColumnIndex(KEY_FAVICON)), cur.getInt(cur.getColumnIndex(KEY_TYPE)), cur.getString(cur.getColumnIndex(KEY_USERNAME)), cur.getString(cur.getColumnIndex(KEY_PASSWORD)));
	}
	
	public List<Booru> getBoorus()
	{
		List<Booru> list = new LinkedList<Booru>();
		Cursor all = fetchAll();
		while(!all.isAfterLast())
		{
			list.add(booruFromCursor(all));
			all.moveToNext();
		}
		all.close();
		return list;
	}
	public long addBooru(String name, String url, int type)
	{
		Map<String, Object> values = new HashMap<String, Object>();
		values.put(KEY_NAME, name);
		values.put(KEY_URL, url);
		values.put(KEY_TYPE, type);
		return createItem(values);
	}
	public void setLoginCredentials(int id, String username, String password)
	{
		Map<String, Object> values = new HashMap<String, Object>();
		Map<String, String> where = new HashMap<String, String>();
		
		values.put(KEY_USERNAME, username);
		values.put(KEY_PASSWORD, password);
		
		where.put(KEY_ID, String.valueOf(id));
		
		updateItem(where, values);
	}
	
	public void setFavicon(int id, String favicon)
	{
		Map<String, Object> values = new HashMap<String, Object>();
		Map<String, String> where = new HashMap<String, String>();
		
		values.put(KEY_FAVICON, favicon);
		
		where.put(KEY_ID, String.valueOf(id));
		
		updateItem(where, values);
	}
}
